package com.ta.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {
    @Value("${order.exchange}")
    private String orderExchange;
    @Value("${order.queue}")
    private String orderQueue;
    @Value("${order.routingKey}")
    private String orderRoutingKey;

    @Value("${delay.exchange}")
    private String delayExchange;
    @Value("${delay.queue}")
    private String delayQueue;
    @Value("${delay.routingKey}")
    private String delayRoutingKey;

    @Value("${dl.exchange}")
    private String dlExchange;
    @Value("${dl.queue}")
    private String dlQueue;
    @Value("${dl.routingKey}")
    private String dlRoutingKey;


    @Bean("orderExchange")
    public DirectExchange orderExchange(){
        return new DirectExchange(orderExchange);
    }

    @Bean("orderQueue")
    public Queue orderQueue(){
        return QueueBuilder.durable(orderQueue).build();

    }

    @Bean
    public Binding bindingOrderExchange(@Qualifier("orderExchange") DirectExchange directExchange,
                                        @Qualifier("orderQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(orderRoutingKey);
    }

    @Bean
    public DirectExchange delayExchange(){
        return new DirectExchange(delayExchange);
    }

    @Bean
    public Queue delayQueue(){
        Map<String, Object> arguments = new HashMap<>(2);
        // 绑定我们的死信交换机
        arguments.put("x-dead-letter-exchange", dlExchange);
        // 绑定我们的路由key
        arguments.put("x-dead-letter-routing-key", dlRoutingKey);
        return new Queue(delayQueue, true, false, false, arguments);

    }

    @Bean
    public Binding bindingOrdExchange() {
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with(delayRoutingKey);
    }

    @Bean
    public DirectExchange dlExchange(){
        return new DirectExchange(dlExchange);
    }

    @Bean
    public Queue dlQueue(){
        return new Queue(dlQueue, true, false, false);
    }


    @Bean
    public Binding bindingDleExchange(){
        return BindingBuilder.bind(dlQueue()).to(dlExchange()).with(dlRoutingKey);
    }

}
